package com.ruoyi.vqms.netty;

import com.ruoyi.vqms.socket.SocketInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

/**
 * netty启动服务端口
 */
@Component
public class SocketServer {

    private EventLoopGroup bossGroup;       // 主线程池
    private EventLoopGroup workerGroup;     // 工作线程池
    private ServerBootstrap server;         // 服务器
    private ChannelFuture future;           // 回调

    public void start() {
        future = server.bind(9006);
        System.out.println("logistics socket server - 启动成功,监听端口：9006");
    }

    public SocketServer() {
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();

        server = new ServerBootstrap();
        server.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childOption(ChannelOption.SO_KEEPALIVE,true)
                .childOption(ChannelOption.TCP_NODELAY, true)
//                .childOption(ChannelOption.SO_BACKLOG,1024)     //等待队列
                .childOption(ChannelOption.SO_REUSEADDR,true)   //快速复用
                .childHandler(new SocketInitializer());
    }
}
